package com.android.vgongyi.dao;

import java.util.ArrayList;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.android.vgongyi.common.AppConstant;
import com.android.vgongyi.entity.CommonEntity;
/**
 * DAO for table Focused
 * */
public class CommonDAO extends BaseDAO{

	private String item_id        = "itemId";
	private String title          = "title";
	private String writer_name    = "writer_name";
	private String weibo_url      = "weibo_url";
	private String img_extension  = "img_extension";
	private String img_id         = "img_id";
	private String big_height     = "big_height";
	private String big_width      = "big_width";
	private String content        = "content";
	private String insert_date    = "insert_date";
	private String img_url        = "img_url";
	private String adlink         = "adlink";
	private String is_showbtn     = "is_showbtn";
	private String ad_btntitle    = "ad_btntitle";
	private String weibo_platform = "weibo_platform";
	private String comment_nums   = "comment_nums";
	private String audit_status   = "audit_status";
	private String last_update    = "last_update";
	//new append
	private String id                = "id";
	private String profile_imageurl  = "profile_imageurl";
	private String weibo_id          = "weibo_id";
	private String verified          = "verified";
	private String verified_reason   = "verified_reason";
	private String is_favourite     = "is_favourite";
	
	private Context context;
	
	public CommonDAO(Context context) {
		
		this.context = context;
		
	}
	
	/**
	 * 	String for executing SQL.
	 * @return String , which is for creating TABLE after being executed.
	 * */
	public String createTableString(){
		
		StringBuffer buffer = new StringBuffer();
		buffer.append("create table ");
		buffer.append(TABLE_COMMON);
		buffer.append("(");
		buffer.append("_id integer primary key AUTOINCREMENT,");
		buffer.append(item_id).append(" text,");
		buffer.append(title).append(" text,");
		buffer.append(writer_name).append(" text,");
		buffer.append(weibo_url).append(" text,");
		buffer.append(img_extension).append(" text,");
		buffer.append(img_id).append(" text,");
		buffer.append(big_height).append(" text,");
		buffer.append(big_width).append(" text,");
		buffer.append(content).append(" text,");
		buffer.append(insert_date).append(" text,");
		buffer.append(img_url).append(" text,");
		buffer.append(adlink).append(" text,");
		buffer.append(is_showbtn).append(" text,");
		buffer.append(ad_btntitle).append(" text,");
		buffer.append(weibo_platform).append(" text,");
		buffer.append(comment_nums).append(" text,");
		buffer.append(audit_status).append(" text,");
		buffer.append(last_update).append(" text,");
		//new append
		buffer.append(profile_imageurl).append(" text,");
		buffer.append(weibo_id).append(" text,");
		buffer.append(verified).append(" text,");
		buffer.append(verified_reason).append(" text,");
		buffer.append(is_favourite).append(" text,");
		buffer.append(id).append(" text unique");
		
		buffer.append(");");
		return buffer.toString();
		
	}
	
	/**
	 * drop table if exists
	 * @return String , for executing ,remove table from database
	 * */

	public String dropTable(){
		
		StringBuffer buffer = new StringBuffer();
		buffer.append(super.DROP_TABLE);
		buffer.append(TABLE_COMMON);
		
		return buffer.toString();
		
	}

	/**
	 * insert data into database
	 * @param obj , completely down-loaded entity info
	 * */
	public void insert(Object obj){
			
		CommonEntity entity      = (CommonEntity)obj;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values     = new ContentValues();
		
		values.put(item_id, entity.getItem_id());
		values.put(title, entity.getTitle());
		values.put(writer_name, entity.getWriter_name());
		values.put(weibo_url, entity.getWeibo_url());
		values.put(img_extension, entity.getImg_extension());
		values.put(img_id, entity.getImg_id());
		values.put(big_height, entity.getBig_height());
		values.put(big_width, entity.getBig_width());
		values.put(content, entity.getContent());
		values.put(insert_date, entity.getInsert_date());
		values.put(img_url, entity.getImg_url());
		values.put(adlink, entity.getAdlink());
		values.put(is_showbtn, entity.getIs_showbtn());
		values.put(ad_btntitle, entity.getAd_btntitle());
		values.put(weibo_platform, entity.getWeibo_platform());
		values.put(comment_nums, entity.getComment_nums());
		values.put(audit_status, entity.getAudit_status());
		values.put(last_update, entity.getLast_update());
		//new append
		values.put(profile_imageurl, entity.getProfile_imageurl());
		values.put(weibo_id, entity.getWeibo_id());
		values.put(verified, entity.getVerified());
		values.put(verified_reason, entity.getVerified_reason());
		values.put(id, entity.getId());
		
		
		Cursor cursor = resolver.query(URI_FAVORITE, null, "id='"+entity.getId()+"'", null, null);
		if(cursor!=null && cursor.getCount()>AppConstant.NO_0){
			values.put(is_favourite, String.valueOf(true));
		}else{
			values.put(is_favourite, String.valueOf(false));
		}
		
		resolver.insert(URI_COMMON, values);
		cursor.close();
		
	}
	
	/**
	 * delete record with conditions
	 * @param where , A filter to apply to rows before deleting, formatted as an SQL WHERE clause (excluding the WHERE itself).
	 * @param selectionArgs , 
	 * */
	public void delete(String where ,String[] selectionArgs){
		
		ContentResolver resolver = context.getContentResolver();
		resolver.delete(URI_COMMON, where, selectionArgs);
		
	}
	
	/**
	 * query records with conditions
	 * @param where , A filter declaring which rows to return, formatted as an SQL WHERE clause (excluding the WHERE itself). Passing null will return all rows for the given URI.
	 * @param selectionArgs , You may include ?s in selection, which will be replaced by the values from selectionArgs, in the order that they appear in the selection. The values will be bound as Strings.
	 * @return ArrayList<Object> , the list of records
	 * */
	public ArrayList<CommonEntity> query(String[] selections,String where, String[] selectionArgs,String sortOrder){
		
		ContentResolver resolver          = context.getContentResolver();
		Cursor cursor                     =  resolver.query(URI_COMMON, selections, where, selectionArgs, null);
		ArrayList<CommonEntity> arrayList = new ArrayList<CommonEntity>();	
		CommonEntity entity               = null;
		
		if(null==cursor) return arrayList;
		
		if(cursor.getCount()<AppConstant.NO_1){
			
			cursor.close();
			return arrayList;
			
		}
		
		int count = cursor.getCount();
		
		if(count>AppConstant.NO_0) cursor.moveToFirst();
		
		do {

			entity = new CommonEntity();
			
			entity.setItem_id(cursor.getString(cursor.getColumnIndex(item_id)));
			entity.setTitle(cursor.getString(cursor.getColumnIndex(title)));
			entity.setWriter_name(cursor.getString(cursor.getColumnIndex(writer_name)));
			entity.setWeibo_url(cursor.getString(cursor.getColumnIndex(weibo_url)));
			entity.setImg_extension(cursor.getString(cursor.getColumnIndex(img_extension)));
			entity.setImg_id(cursor.getString(cursor.getColumnIndex(img_id)));
			entity.setBig_height(cursor.getString(cursor.getColumnIndex(big_height)));
			entity.setBig_width(cursor.getString(cursor.getColumnIndex(big_width)));
			entity.setContent(cursor.getString(cursor.getColumnIndex(content)));
			entity.setInsert_date(cursor.getString(cursor.getColumnIndex(insert_date)));
			entity.setImg_url(cursor.getString(cursor.getColumnIndex(img_url)));
			entity.setAdlink(cursor.getString(cursor.getColumnIndex(adlink)));
			entity.setIs_showbtn(cursor.getString(cursor.getColumnIndex(is_showbtn)));
			entity.setAd_btntitle(cursor.getString(cursor.getColumnIndex(ad_btntitle)));
			entity.setWeibo_platform(cursor.getString(cursor.getColumnIndex(weibo_platform)));
			entity.setComment_nums(cursor.getString(cursor.getColumnIndex(comment_nums)));
			entity.setAudit_status(cursor.getString(cursor.getColumnIndex(audit_status)));
			entity.setLast_update(cursor.getString(cursor.getColumnIndex(last_update)));
			//new append
			entity.setId(cursor.getString(cursor.getColumnIndex(id)));
			entity.setProfile_imageurl(cursor.getString(cursor.getColumnIndex(profile_imageurl)));
			entity.setWeibo_id(cursor.getString(cursor.getColumnIndex(weibo_id)));
			entity.setVerified(cursor.getString(cursor.getColumnIndex(verified)));
			entity.setVerified_reason(cursor.getString(cursor.getColumnIndex(verified_reason)));
			entity.setFavourite(Boolean.valueOf(cursor.getString(cursor.getColumnIndex(is_favourite))));
			arrayList.add(entity);
			
		} while (cursor.moveToNext());
		
		cursor.close();
		
		return arrayList;
	}
	
	/**
	 * update and reset the records
	 * @param obj,
	 * @param where,  A filter to apply to rows before deleting, formatted as an SQL WHERE clause (excluding the WHERE itself).
	 * @param selectionArgs,
	 * */
	public void update(Object obj,String where,String[] selectionArgs){
		
		CommonEntity entity      = (CommonEntity )obj;
		ContentResolver resolver = context.getContentResolver();
		ContentValues values     = new ContentValues();
		
		values.put(item_id, entity.getItem_id());
		values.put(title, entity.getTitle());
		values.put(writer_name, entity.getWriter_name());
		values.put(weibo_url, entity.getWeibo_url());
		values.put(img_extension, entity.getImg_extension());
		values.put(img_id, entity.getImg_id());
		values.put(big_height, entity.getBig_height());
		values.put(big_width, entity.getBig_width());
		values.put(content, entity.getContent());
		values.put(insert_date, entity.getInsert_date());
		values.put(img_url, entity.getImg_url());
		values.put(adlink, entity.getAdlink());
		values.put(is_showbtn, entity.getIs_showbtn());
		values.put(ad_btntitle, entity.getAd_btntitle());
		values.put(weibo_platform, entity.getWeibo_platform());
		values.put(comment_nums, entity.getComment_nums());
		values.put(audit_status, entity.getAudit_status());
		values.put(last_update, entity.getLast_update());
		//new append
		values.put(profile_imageurl, entity.getProfile_imageurl());
		values.put(weibo_id, entity.getWeibo_id());
		values.put(verified, entity.getVerified());
		values.put(verified_reason, entity.getVerified_reason());
		values.put(is_favourite, String.valueOf(entity.isFavourite()));
		values.put(id, entity.getId());
		
		resolver.update(URI_COMMON, values, where, selectionArgs);
	}
}
